<template>
  <div>
    <el-dialog
      :close-on-click-modal="false"
      v-model="visibleDialog"
      :title="props.title"
      width="800px"
      draggable
      :align-center="true"
      @close="closeDialog"
      destroy-on-close
      style="min-width: 850px"
      class="maxhDialog"
    >
      <el-descriptions :column="2" size="large" border v-if="props.isReadonly">
        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">设备编号</div>
          </template>
          {{ formData.equipCode || "" }}
        </el-descriptions-item>
        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">故障开始时间</div>
          </template>
          {{ formData.beginTimeLocal || "" }}
        </el-descriptions-item>
        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">故障信息</div>
          </template>
          {{ formData.faultText || "" }}
        </el-descriptions-item>
        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">故障时间(小时)</div>
          </template>
          {{ formData.faultDuration || "" }}
        </el-descriptions-item>
        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">分析状态</div>
          </template>
          {{ formData.analysisStatus || "" }}
        </el-descriptions-item>
        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">故障部位</div>
          </template>
          {{ formData.faultPosition || "" }}
        </el-descriptions-item>

        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">故障现象</div>
          </template>
          {{ formData.faultPhenomenon || "" }}
        </el-descriptions-item>

        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">故障原因</div>
          </template>
          {{ formData.faultReason || "" }}
        </el-descriptions-item>

        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">创建时间</div>
          </template>
          {{ formData.createTime || "" }}
        </el-descriptions-item>

        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">创建人姓名</div>
          </template>
          {{ formData.createdUserName || "" }}
        </el-descriptions-item>

        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">更新人姓名</div>
          </template>
          {{ formData.lastUpdatedUserName || "" }}
        </el-descriptions-item>

        <el-descriptions-item :span="1">
          <template #label>
            <div class="cell-item">最后更新时间</div>
          </template>
          {{ formData.lastUpdateTime || "" }}
        </el-descriptions-item>
      </el-descriptions>
      <el-form
        ref="formRef"
        v-if="!props.isReadonly"
        :model="formData"
        :rules="rules"
        label-width="160"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备编号" prop="equipCode">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.equipCode"
                placeholder="请输入设备编号"
                clearable
              />

              <span v-else>{{ formData.equipCode }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="故障开始时间" prop="beginTimeLocal">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.beginTimeLocal"
                placeholder="请输入故障开始时间"
                clearable
              />

              <span v-else>{{ formData.beginTimeLocal }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="故障信息" prop="faultText">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.faultText"
                placeholder="请输入故障信息"
                clearable
              />

              <span v-else>{{ formData.faultText }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="故障时间(小时)" prop="faultDuration">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.faultDuration"
                placeholder="请输入故障时间(小时)"
                clearable
              />

              <span v-else>{{ formData.faultDuration }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分析状态" prop="analysisStatus">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.analysisStatus"
                placeholder="请输入分析状态"
                clearable
              />

              <span v-else>{{ formData.analysisStatus }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="故障部位" prop="faultPosition">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.faultPosition"
                placeholder="请输入故障部位"
                clearable
              />

              <span v-else>{{ formData.faultPosition }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="故障现象" prop="faultPhenomenon">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.faultPhenomenon"
                placeholder="请输入故障现象"
                clearable
              />

              <span v-else>{{ formData.faultPhenomenon }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="故障原因" prop="faultReason">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.faultReason"
                placeholder="请输入故障原因"
                clearable
              />

              <span v-else>{{ formData.faultReason }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间" prop="createTime">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.createTime"
                placeholder="请输入创建时间"
                clearable
              />

              <span v-else>{{ formData.createTime }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建人姓名" prop="createdUserName">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.createdUserName"
                placeholder="请输入创建人姓名"
                clearable
              />

              <span v-else>{{ formData.createdUserName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="更新人姓名" prop="lastUpdatedUserName">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.lastUpdatedUserName"
                placeholder="请输入更新人姓名"
                clearable
              />

              <span v-else>{{ formData.lastUpdatedUserName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最后更新时间" prop="lastUpdateTime">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.lastUpdateTime"
                placeholder="请输入最后更新时间"
                clearable
              />

              <span v-else>{{ formData.lastUpdateTime }}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button
            v-if="!props.isReadonly"
            type="primary"
            :loading="loading"
            @click="handleSubmit"
            >确 定</el-button
          >
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "ETLEQUFAULTINFOADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      if (formRef.value?.clearValidate) {
        formRef.value && formRef.value.clearValidate();
      }
    }, 50);
  }
);

const rules = reactive({
  equipCode: [],
  beginTimeLocal: [],
  faultText: [],
  faultDuration: [],
  analysisStatus: [],
  faultPosition: [],
  faultPhenomenon: [],
  faultReason: [],
  createTime: [],
  createdUserName: [],
  lastUpdatedUserName: [],
  lastUpdateTime: [],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  equipCode: "",
  beginTimeLocal: "",
  faultText: "",
  faultDuration: "",
  analysisStatus: "",
  faultPosition: "",
  faultPhenomenon: "",
  faultReason: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  lastUpdateTime: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        equipCode,
        beginTimeLocal,
        faultText,
        faultDuration,
        analysisStatus,
        faultPosition,
        faultPhenomenon,
        faultReason,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdatedUserId,
        lastUpdatedUserName,
        lastUpdateTime,
      } = newVal;
      Object.assign(formData, {
        id,
        equipCode,
        beginTimeLocal,
        faultText,
        faultDuration,
        analysisStatus,
        faultPosition,
        faultPhenomenon,
        faultReason,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdatedUserId,
        lastUpdatedUserName,
        lastUpdateTime,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  if (formRef.value?.resetFields) {
    formRef.value.resetFields();
    formRef.value.clearValidate();
  }
  formData.id = "";
  formData.equipCode = "";
  formData.beginTimeLocal = "";
  formData.faultText = "";
  formData.faultDuration = "";
  formData.analysisStatus = "";
  formData.faultPosition = "";
  formData.faultPhenomenon = "";
  formData.faultReason = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.lastUpdateTime = "";
}

const loading = ref(false);

/** 设备故障信息保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/pqd-etl/equfaultinfo/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>

<style lang="scss" scoped>
:deep(.maxhDialog .el-dialog__body) {
  max-height: 70vh;
  overflow-y: auto;
  padding: 20px;
}
:deep(.el-descriptions__label.el-descriptions__cell.is-bordered-label) {
  width: 130px;
}
:deep(.el-descriptions__content.el-descriptions__cell.is-bordered-content) {
  min-width: 180px;
}
</style>
